const mysql = require('mysql');

//1.创建连接对象
let connection = mysql.createConnection({
    host:'localhost',
    port:3306,
    user:'root',
    password:'root',
    database:'1708_userlist'  //数据库
})

module.exports = (sql) => {
    //2.连接数据库
    connection.connect((error) => {
        if(error){
            console.log("数据库连接失败")
        }else{
            console.log("数据库连接成功")
        }
    })

    //和数据库交互的过程是异步的
    return new Promise((resolve,reject) => {
        console.log("======");
        //3.增删改查
        //query 第一个参数是sql语句
        connection.query(sql,(error,data) => {
            if(error){
                reject(error)
            }else{
                resolve(data);
            }
            //4.关闭链接
            connection.end();
        })
    })
}